package q0232;

import java.util.Stack;

/**
 * @author wangxiyue@cyou-inc.com
 * @date 2019-09-04
 * @since 1.0.0
 */
public class MyQueue {

    private Stack<Integer> stack_in;
    private Stack<Integer> stack_out;

    /**
     * Initialize your data structure here.
     */
    public MyQueue() {
        stack_in = new Stack();
        stack_out = new Stack();
    }

    /**
     * Push element x to the back of queue.
     */
    public void push(int x) {
        stack_in.push(x);
    }

    /**
     * Removes the element from in front of queue and returns that element.
     */
    public int pop() {
        if (!stack_out.isEmpty()) {
            return stack_out.pop();
        }
        while (!stack_in.isEmpty()) {
            stack_out.push(stack_in.pop());
        }
        return stack_out.pop();
    }


    /**
     * Get the front element.
     */
    public int peek() {
        if (!stack_out.isEmpty()) {
            return stack_out.peek();
        }
        while (!stack_in.isEmpty()) {
            stack_out.push(stack_in.pop());
        }
        return stack_out.peek();
    }

    /**
     * Returns whether the queue is empty.
     */
    public boolean empty() {
        return stack_in.isEmpty() && stack_out.isEmpty();
    }


    public static void main(String[] args) {
//        test1();
//        test2();
//        test3();
        test4();
    }

    private static void test4() {
        MyQueue myQueue = new MyQueue();
        myQueue.push(1);
        myQueue.push(2);
        System.out.println("1=" + myQueue.pop());
        System.out.println("2=" + myQueue.peek());
    }

    private static void test3() {
        MyQueue myQueue = new MyQueue();
        myQueue.push(1);
        myQueue.push(2);
        System.out.println("1=" + myQueue.peek());
        System.out.println("1=" + myQueue.pop());
        System.out.println("false=" + myQueue.empty());
    }

    private static void test2() {
        MyQueue myQueue = new MyQueue();
        myQueue.push(1);
        myQueue.push(2);
        System.out.println("1=" + myQueue.peek());
        myQueue.push(3);
        System.out.println("1=" + myQueue.peek());
    }

    private static void test1() {
        MyQueue myQueue = new MyQueue();
        myQueue.push(1);
        myQueue.push(2);
        myQueue.push(3);
        System.out.println("1=" + myQueue.peek());
    }
}
